﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ProlicySocketServer
{
    /// <summary>
    /// Window1.xaml 的交互逻辑
    /// </summary>
    public partial class Window1 : Window
    {

        /// <summary>
        /// 声明一个线程
        /// </summary>
        System.Threading.Thread Thr = null;

        public Window1()
        {
            InitializeComponent();

        }

        /// <summary>
        /// 事件代码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void procliySocketEntity_MessageChanged(object sender, ProcliySocketOutMessageEventArgs e)
        {

            ///线程调用UI
            Dispatcher.BeginInvoke(
                (System.Threading.ThreadStart)delegate()
                {

                    if ((label1.Content as string).Split('\r').Length >15)
                    {
                        label1.Content = "";
                    }

                    label1.Content += e.OutMessage+"\r\n";

                },
                 System.Windows.Threading.DispatcherPriority.Normal, null);
        }

        /// <summary>
        /// StopSocketServer 对像
        /// </summary>
        ProcliySocketEntity procliySocketEntity = null;
        
        /// <summary>
        /// 运行
        /// </summary>
        void Run()
        {
            procliySocketEntity = new ProcliySocketEntity();
            procliySocketEntity.MessageChanged += new ProcliySocketOutMessage(procliySocketEntity_MessageChanged);
            procliySocketEntity.StartSocketServer();
        }

        /// <summary>
        /// 调用运行代码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, RoutedEventArgs e)
        {

            Thr = new System.Threading.Thread(new System.Threading.ThreadStart(Run));
            Thr.Start();

        }

        /// <summary>
        /// 关闭时发生
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Window_Closed(object sender, EventArgs e)
        {
            if (procliySocketEntity != null)
            {
               // procliySocketEntity.StopSocketServer();
            }
        }

        /// <summary>
        /// 停止对 产品的监听
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, RoutedEventArgs e)
        {
            if (procliySocketEntity != null)
            {
                procliySocketEntity.StopSocketServer();
            }
        }


    }
}
